/*
 * CDocument.cpp
 *
 *  Created on: Sep 6, 2010
 *      Author: data
 */

#include "CDocument.h"
#include "../include/CUtilities.h"
using namespace std;

/**
 *
 */
CDocument::CDocument(string resource_name ) {
	_p_token_list = new CTokenList();
	_resource_name = resource_name;
}

/**
 *
 */
CDocument::~CDocument() {
	delete _p_token_list;

	// Cleanup
	tokenTermMap.clear();
	semWikiData.clear();
	semWikiTerms.clear();
	catTermIds.clear();
	//delete tokenTermMap;
	//delete semWikiData;
	//delete semWikiTerms;
	//tokenPosMap.clear();
}

/**
 *
 */
bool CDocument::add_token(string token) {
	return _p_token_list->add_token(token);
}

/**
 *
 */
bool CDocument::delete_token(string token) {
	return _p_token_list->delete_token(token);
}

/**
 * @brief
 * @note This function should do a recursive traversal
 */
void CDocument::process_token_list(CBasicTokenProcessor& token_processor) {
	list<CToken*>::iterator itr;
	for ( itr = _p_token_list->get_token_list()->begin() ; itr != _p_token_list->get_token_list()->end(); ++itr )
		(*itr)->set_token_normal_string(token_processor.process_token((*itr)->get_token_raw_string()));
	return ;
}

int CDocument::getNoOfReferences() const
{
    return no_of_references;
}

int CDocument::getNoOfSections() const
{
    return no_of_sections;
}

void CDocument::setNoOfReferences(int noOfReferences)
{
    no_of_references = noOfReferences;
}


vector<size_t> CDocument::getCatTermIds() const
{
    return catTermIds;
}

size_t CDocument::getAllTermsCount() const
{
    return allTermsCount;
}

size_t CDocument::getJcIntersectCount() const
{
    return JCIntersectCount;
}

void CDocument::setAllTermsCount(size_t allTermsCount)
{
    this->allTermsCount = allTermsCount;
}

void CDocument::setJcIntersectCount(size_t jcIntersectCount)
{
    JCIntersectCount = jcIntersectCount;
}

void CDocument::setCatTermIds(vector<size_t> catTermIds)
{
    this->catTermIds = catTermIds;
}

void CDocument::setNoOfSections(int noOfSections)
{
    no_of_sections = noOfSections;
}

/**
 * @brief
 * @return a string representation of the document
 */
string CDocument::to_string() {
	string s = "";
	s += _resource_name + ", ";
	s += CUtilities::number_to_string(_doc_classification_type) + ", [";
	map<string, string>::iterator itr;
	for ( itr = _doc_feature_map.begin(); itr != _doc_feature_map.end(); ++itr )
		s += itr->first + " : " + itr->second + ", ";
	s += "] ";
	return s;
}

/**
 *
 */
CToken::CToken(std::string token, CTokenList* p_current_token_list) {
	_token_raw_string = token;
	_token_normal_form = "";
//	CTokenList *_p_child_token_list = 0;
//	CToken *_p_parent_token = 0;
	_p_current_token_list = p_current_token_list;
}

/**
 *
 */
CTokenList::CTokenList() {
	_p_token_list = new list<CToken*>();
}

/**
 * @brief destructor for CTokenList
 */
CTokenList::~CTokenList() {
	destroy_token_list(_p_token_list);
}

/**
 * @brief destroy the token list
 */
void CTokenList::destroy_token_list(list<CToken*> *head) {
	// do a bfs for complete token list destruction
	list<CToken*>::iterator itr;
	for ( itr = head->begin(); itr != head->end(); ++itr ) {
		if ( (*itr)->get_child_token_list()->get_token_list()->size() != 0 )
			destroy_token_list((*itr)->get_child_token_list()->get_token_list());
		else
			delete *itr;
	}

}

/**
 *
 */
bool CTokenList::add_token(string token) {
	bool status = false;
	_p_token_list->push_back(new CToken(token, this));
	status = true;
	return status;
}

/**
 *
 */
bool CTokenList::delete_token(std::string token) {
	bool status = false;
	// deleting a token is tricky if its child is not null
	return status;
}

/**
 *
 */
bool CTokenList::split_token(CToken* p_token, std::string splitter_string) {
	bool status = false;
	return status;
}
